import {
  getDevicePropertyDataTypeList,
  getDeviceList,
  getDeviceTypeList,
} from '@/api/collection/device'
import { getDataUnitTree, getUnitGroupList, getUnitList } from '@/api/collection/unit'
import { BasicColumn, FormSchema } from '@/components/Table'
import { TreeSelect } from 'ant-design-vue'

export const columns: BasicColumn[] = [
  {
    title: '设备名称',
    dataIndex: 'deviceName',
  },
  {
    title: '设备编码',
    dataIndex: 'deviceCode',
  },
  {
    title: '设备参数',
    dataIndex: 'dataTypeName',
  },
  {
    title: '参数类型',
    dataIndex: 'calculateType',
    customRender: ({ record }) => {
      let retMsg = ''
      switch (record.calculateType) {
        case 1:
          retMsg = '实时值'
          break
        case 2:
          retMsg = '增量值'
          break
        case 3:
          retMsg = '实时增量值'
          break
        default:
          retMsg = '辅助计算值'
      }
      return retMsg
    },
  },
  {
    title: '倍率系数',
    dataIndex: 'multiple',
  },
]

export const searchFormSchema: FormSchema[] = [
  {
    field: 'deviceName',
    label: '设备名称',
    component: 'Input',
    colProps: { span: 6 },
  },
]

export const classifyFormSchema: FormSchema[] = [
  {
    label: '设备类型',
    field: 'deviceTypeId',
    component: 'ApiSelect',
    componentProps: ({ formModel, formActionType }) => {
      return {
        api: getDeviceTypeList,
        labelField: 'name',
        valueField: 'id',
        onChange: async (e, v) => {
          const ret = await getDeviceList({
            type: v.code,
          })
          const floorOptions = ret.map((item) => ({
            value: item.id,
            label: item.name,
            key: item.id,
          }))
          formModel.deviceId = undefined //  reset deviceId value
          const { updateSchema } = formActionType
          updateSchema({
            field: 'deviceId',
            componentProps: {
              options: floorOptions,
              showSearch: true,
              optionFilterProp: 'label',
              onChange: async (e, v) => {
                console.log(e, v)
                const dtItem = await getDevicePropertyDataTypeList({ deviceId: e })
                const dataTypeItemOptions = dtItem.map((item) => ({
                  value: item.id,
                  label: item.name,
                  key: item.id,
                  dictItemList: item.dictItemList,
                }))
                formModel.dataTypeId = undefined
                const { updateSchema } = formActionType
                updateSchema({
                  field: 'dataTypeId',
                  componentProps: {
                    options: dataTypeItemOptions,
                  },
                })
              },
            },
          })
        },
      }
    },
    required: true,
  },
  {
    field: 'deviceId',
    label: '设备名称',
    component: 'Select',
    componentProps: {
      options: [],
      showSearch: true,
      optionFilterProp: 'label',
    },
    required: true,
  },
  {
    label: '设备参数',
    field: 'dataTypeId',
    component: 'Select',
    componentProps: {
      options: [],
    },
    required: true,
  },
  {
    label: '参数类型',
    field: 'calculateType',
    component: 'Select',
    componentProps: {
      options: [
        {
          label: '实时值',
          value: 1,
          key: '1',
        },
        {
          label: '增量值',
          value: 2,
          key: '2',
        },
        {
          label: '实时增量值',
          value: 3,
          key: '3',
        },
        // {
        //   label: '辅助计算值',
        //   value: 3,
        //   key: '3',
        // },
      ],
    },
    required: true,
  },
  {
    label: '倍率系数',
    field: 'multiple',
    component: 'InputNumber',
    required: true,
  },
]

export const classifyTypeFormSchema: FormSchema[] = [
  {
    field: 'classifyId',
    label: '分类ID',
    component: 'Input',
    componentProps: {
      disabled: true,
    },
  },
  {
    field: 'classifyNameCopy',
    label: '分类名称',
    component: 'Input',
    required: true,
  },
  {
    field: 'sort',
    label: '排序',
    component: 'InputNumber',
    required: true,
  },
  {
    field: 'multiple',
    label: '倍率系数',
    component: 'InputNumber',
    required: true,
  },
  {
    field: 'countType',
    label: '计算方式',
    component: 'Select',
    componentProps: {
      options: [
        {
          label: '下级分类合计',
          value: 0,
          key: '1',
        },
        {
          label: '下级点位合计',
          value: 1,
          key: '2',
        },
      ],
    },
    required: true,
  },
  {
    field: 'energyType',
    label: '能源类型',
    component: 'ApiSelect',
    componentProps: ({ formModel, formActionType }) => {
      return {
        api: getUnitGroupList,
        labelField: 'name',
        valueField: 'id',
        onChange: async (e, v) => {
          const ret = await getUnitList({
            groupName: e,
          })
          const floorOptions = ret.map((item) => ({
            value: item.id,
            label: item.name,
            key: item.id,
          }))
          // formModel.unitId = undefined //  reset unitId value
          const { updateSchema } = formActionType
          updateSchema({
            field: 'unitId',
            componentProps: {
              options: floorOptions,
            },
          })
        },
      }
    },
  },
  {
    field: 'unitId',
    label: '计量单位',
    // component: 'ApiSelect',
    // componentProps: ({ formModel, formActionType }) => {
    //   return {
    //     api: getUnitGroupList,
    //     labelField: 'name',
    //     valueField: 'id',
    //   }
    // },
    component: 'Select',
    componentProps: {
      options: [],
    },
  },
  {
    field: 'description',
    label: '描述',
    component: 'InputTextArea',
    componentProps: {
      autoSize: { minRows: 3, maxRows: 5 },
    },
  },
]
